package xworker.javafx.event;

import javafx.event.Event;
import javafx.event.EventHandler;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.lang.executor.Executor;

/**
 * 实现EventHandler，执行模型的doAction方法。
 * @param <T>
 */
public class SimpleThingEventHandler<T extends Event> implements EventHandler<T> {
    private static final String TAG = SimpleThingEventHandler.class.getName();

    Thing thing;
    ActionContext actionContext;

    public SimpleThingEventHandler(Thing thing, ActionContext actionContext){
        this.thing = thing;
        this.actionContext = actionContext;
    }

    @Override
    public void handle(T event) {
        try{
            thing.doAction("doAction", actionContext, "event", event);
        }catch(Exception e){
            Executor.warn(TAG, "Execute event handler error, path=" + thing.getMetadata().getPath(), e);
        }
    }
}
